<template>
    <div class="region">
        <div class="content">
            <div class="left">地区:</div>
            <ul class="hospital">
                <li :class="{active:regionFlag==''}" @click="changeLevel('')">全部</li>
                <li v-for="level in regionArr" :class="{active:regionFlag==level.value}" :key="level.value" @click="changeLevel(level.value)">{{ level.name }}</li>
            </ul>
        </div>
    </div>
</template>

<script setup lang="ts">
import {reqHospitalLevelAndRegion} from '@/api/home/index';
import { onMounted,ref } from 'vue';
import type {HospitalLevelAndRegionResponseData,HospitalLevelAndRegionArr} from '@/api/home/type'

// 定义一个数组存储地区数据
let regionArr =ref<HospitalLevelAndRegionArr>([]);
// 控制地区高亮响应式数据
let regionFlag = ref<string>('');
// 地区组件挂载完毕获取地区组件
onMounted(()=>{
    getRegion();
})

// 获取地区的数据
const getRegion =async ()=>{
    // result:HospitalLevelAndRegionResponseData 添加约束类型
    let result:HospitalLevelAndRegionResponseData=await reqHospitalLevelAndRegion('Beijin');
    // console.log(result);
    if(result.data.code==200){
        regionArr.value=result.data.data;
    }
}

// 点击地区按钮回调
const changeLevel = (region:string)=>{
    // 高亮响应式数据存储level数值,实现点击谁让谁高亮
    regionFlag.value=region;
    // 给父组件传递区域的参数
    $emit('getRegion',region);
}

// 触发自定义事件
let $emit = defineEmits(['getRegion']);
</script>

<style scoped lang="scss">
.region{
    color: #939393;
    .content{
        display: flex;
        margin: 20px 0px;
        .left{
            margin-right: 20px;
            width: 48px;
        }
        .hospital{
            display: flex;
            // 换行
            flex-wrap: wrap;
            li{
                margin-right: 10px;
                margin-bottom: 10px;
                &.active{
                    color: #55a6fe;
                }
            }
            li:hover{
                color: #55a6fe;
                cursor: pointer;
            }
        }
    }
}
</style>